package com.example.activiti.service.impl;

import com.example.activiti.model.ActUser;
import com.example.activiti.model.LoginDTO;
import com.example.activiti.service.ActUserService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ActUserServiceImpl implements ActUserService {

    @Resource
    private IdentityService identityService;


    @Override
    public ActUser selectByPrimaryKey(String id) {
        User user = identityService.createUserQuery().userId(id).singleResult();
        ActUser actUser = new ActUser();
        actUser.setId(user.getId());
        actUser.setName(user.getFirstName()+user.getLastName());
        return actUser;
    }

    @Override
    public ActUser findUser(LoginDTO loginDTO) {
        boolean valid = identityService.checkPassword(loginDTO.getUsername(), loginDTO.getPassword());
        if (valid) {
            User user = identityService.createUserQuery().userId(loginDTO.getUsername()).singleResult();;
            ActUser actUser = new ActUser();
            actUser.setId(user.getId());
            actUser.setName(user.getFirstName()+user.getLastName());
            return actUser;
        }
        throw new RuntimeException("用户名或密码错误");
    }
}
